package com.ruoyi.iot.controller;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.iot.domain.IotModelTypeEntity;
import com.ruoyi.iot.domain.MetadataTypeEntity;
import com.ruoyi.iot.service.DataMetaDbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 第三方服务配置Controller
 *
 * @author rww
 * @date 2023-09-22
 */
@RestController
@RequestMapping("/iot/api/config")
public class IotApiConfigController extends BaseController
{
    @Autowired
    private DataMetaDbService dataMetaDbService;

    //获取设备类型
    @GetMapping("/types/list")
    public AjaxResult listIotTypes()
    {
        List<MetadataTypeEntity> metadataTypes = dataMetaDbService.
                selectMetaTypes(MetadataTypeEntity.Type_iotType);
        List<IotModelTypeEntity> iotTypes = dataMetaDbService.selectIotTypes();
        JSONObject data = new JSONObject();
        data.put("metadataTypes",metadataTypes);
        data.put("iotTypes",iotTypes);
        return AjaxResult.success(data);
    }
    public List<MetadataTypeEntity> buildDeptTree(List<MetadataTypeEntity> metadataTypes)
    {
        List<MetadataTypeEntity> returnList = new ArrayList<MetadataTypeEntity>();
        List<String> tempList = metadataTypes.stream().map(MetadataTypeEntity::getId).collect(Collectors.toList());
        for (MetadataTypeEntity dept : metadataTypes)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(dept.getPid()))
            {
                recursionFn(metadataTypes, dept);
                returnList.add(dept);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = metadataTypes;
        }
        return returnList;
    }
    private void recursionFn(List<MetadataTypeEntity> list, MetadataTypeEntity t)
    {
        // 得到子节点列表
        List<MetadataTypeEntity> childList = getChildList(list, t);
        t.setChildren(childList);
        for (MetadataTypeEntity tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }
    private List<MetadataTypeEntity> getChildList(List<MetadataTypeEntity> list, MetadataTypeEntity t)
    {
        List<MetadataTypeEntity> tlist = new ArrayList<MetadataTypeEntity>();
        Iterator<MetadataTypeEntity> it = list.iterator();
        while (it.hasNext())
        {
            MetadataTypeEntity n = (MetadataTypeEntity) it.next();
            if (StringUtils.isNotNull(n.getPid()) && n.getPid().equals(t.getId()))
            {
                tlist.add(n);
            }
        }
        return tlist;
    }
    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<MetadataTypeEntity> list, MetadataTypeEntity t)
    {
        return getChildList(list, t).size() > 0;
    }
}
